package com.lmx.persistence.service;

import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.common.BaseCrudServiceImpl;
import com.lmx.repoDomain.tmodel.AssessmentIndicator;
import com.lmx.repoDomain.tmodel.TempAssessmentIndicators;

/**
 * 员工考核抽查Service实现
 *
 * @author ytche
 * @date 2017年9月5日
 *
 */
@Service
public class TempAssessmentIndicatorsServiceImpl extends
		BaseCrudServiceImpl<TempAssessmentIndicators> implements TempAssessmentIndicatorsService {

	@Override
	public List<AssessmentIndicator> findForTempAssessment(Integer assessmentDate) {
		Integer startTime = assessmentDate == null ? CommonUtil.getWeekBegin(new Date(), true)
				: assessmentDate;
		Integer endTime = 7 * 24 * 3600 + startTime;
		Long tenantId = ActionContextHolder.getContext().getTenantId();
		String queryStr = "From TempAssessmentIndicators where delStatus=0 "
				+ " and tenant.id=:tenantId and assessmentDate>=:startTime and "
				+ "assessmentDate<:endTime ";
		List<TempAssessmentIndicators> list = super.em.createQuery(queryStr)
				.setParameter("tenantId", tenantId).setParameter("startTime", startTime)
				.setParameter("endTime", endTime).getResultList();
		return list == null || list.size() < 1 ? null : Lists.newArrayList(list.get(0)
				.getIndicators());
	}

	@Override
	public TempAssessmentIndicators create(TempAssessmentIndicators entity) {
		if (entity.getAssessmentDate() == null) {
			entity.setAssessmentDate(CommonUtil.getWeekBegin(new DateTime().plusWeeks(1).toDate(),
					true));
		}
		return super.create(entity);
	}
}
